package com.taoyibao.hongcanting.ui.mine.activity;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.taoyibao.hongcanting.R;
import com.taoyibao.hongcanting.base.activity.BaseActivityPresenter;
import com.taoyibao.hongcanting.cache.SpUtils;
import com.taoyibao.hongcanting.constant.CodeConstant;
import com.taoyibao.hongcanting.constant.NetCode;
import com.taoyibao.hongcanting.event.EventFinishActivity;
import com.taoyibao.hongcanting.http.DefaultTransformer;
import com.taoyibao.hongcanting.http.JsonApiWrapper;
import com.taoyibao.hongcanting.http.ProgressSubscriber;
import com.taoyibao.hongcanting.model.CompanyBaseModel;
import com.taoyibao.hongcanting.ui.mine.delegate.SetPayPwdStepOneDelegate;
import com.taoyibao.hongcanting.utils.CodeUtils;
import com.taoyibao.hongcanting.utils.ToastUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/**
 * @author TangAnna
 * @description: 设置支付密码第一步
 * @date :${DATA} 16:30
 */
public class SetPayPwdStepOneActivity extends BaseActivityPresenter<SetPayPwdStepOneDelegate> implements View.OnClickListener {

    /**
     * 页面Title
     */
    private String mTitle;

    @Override
    protected Class<SetPayPwdStepOneDelegate> getDelegateClass() {
        return SetPayPwdStepOneDelegate.class;
    }

    /**
     * @param context
     * @param title   不同的业务的title
     */
    public static void open(Context context, String title) {
        Intent intent = new Intent(context, SetPayPwdStepOneActivity.class);
        intent.putExtra(CodeConstant.TITLE, title);
        context.startActivity(intent);
    }

    @Override
    protected void bindEvenListener() {
        super.bindEvenListener();
        EventBus.getDefault().register(this);
        mTitle = getIntent().getStringExtra(CodeConstant.TITLE);
        viewDelegate.setTitle(mTitle);
        viewDelegate.get(R.id.tv_setPayPwd_getVCode).setOnClickListener(this);
        viewDelegate.get(R.id.btn_setPayPwd_confirm).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_setPayPwd_getVCode://发送验证码
                getVcode();
                break;
            case R.id.btn_setPayPwd_confirm://确定
                if (!TextUtils.isEmpty(viewDelegate.getVCode())) {
                    checkVCode();
                } else {
                    ToastUtils.showToast(CodeUtils.getIdString(R.string.string_hintInputVCode));
                }
                break;
        }
    }

    /**
     * 获取验证码
     */
    public void getVcode() {
        JsonApiWrapper.getApiService().sendCode(SpUtils.getUserPhone())
                .compose(new DefaultTransformer<CompanyBaseModel>())
                .subscribe(new ProgressSubscriber<CompanyBaseModel>(this, true) {
                    @Override
                    public void onNext(CompanyBaseModel result) {
                        if (result.code == NetCode.HTTP_200) {
                            CodeUtils.setSecurityTextView((TextView) viewDelegate.get(R.id.tv_setPayPwd_getVCode));
                        } else {
                            super.onNext(result);
                        }
                    }
                });
    }

    /**
     * 验证验证码
     */
    public void checkVCode() {
        JsonApiWrapper.getApiService().checkCode(SpUtils.getToken(), SpUtils.getUserPhone(), viewDelegate.getVCode())
                .compose(new DefaultTransformer<CompanyBaseModel>())
                .subscribe(new ProgressSubscriber<CompanyBaseModel>(this, true) {
                    @Override
                    public void onNext(CompanyBaseModel companyBaseModel) {
                        if (companyBaseModel.code == NetCode.HTTP_200) {
                            SetPayPwdStepTwoActivity.open(SetPayPwdStepOneActivity.this, mTitle);
                        } else {
                            super.onNext(companyBaseModel);
                        }
                    }
                });
    }

    /**
     * 第二步成功之后 收消息关闭页面
     *
     * @param event
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void successEvent(EventFinishActivity event) {
        if (event.setPayPedSuccess) {
            finish();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
